<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core\lang;
use classlib\core as C;
use classlib\core\controller;
use src\logic\workflow\doc_executor;
class Cindex extends controller {
	//工作流主线程
	public function main() {
		$view = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$id   = (int)input::get('id');
		$data = C\model::load('main.proc.workflow')->get_fields();
		if ($id > 0) {
			$data = C\model::load('main.proc.workflow')->get_one($id);
		}
		if (!$data['clasz']) {
			$data['clasz'] = 'src\\logic\\workflow\\';
		}
		$view->assign_by_ref('data', $data);
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('workflow/design.ext.js');
	}
	
	//我的工作流审批单据
	public function mytask() {
		$view  = C\view::inst('json');
		$userid= Cadmin::islogin($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$dlang = lang::inst('dd');
		$where = array('userid'=>$userid);
		$rorm  = C\model::load('main.sys.ruser');
		$rids  = $rorm->get_column(0, -1, $where, 'ruleid');
		$rids  = empty($rids)? '0':implode(',', $rids);
		$oorm  = C\model::load('main.sys.organize');
		$where = array('chargeuid'=>$userid);
		$cgids = $oorm->get_column(0, -1, $where, 'id');
		$cgids = empty($cgids)? '0':implode(',', $cgids);
		$where = array('prechargeuid'=>$userid);
		$pgids = $oorm->get_column(0, -1, $where, 'id');
		$pgids = empty($pgids)? '0':implode(',', $pgids);
		$sql   = 'SELECT %s FROM proc_nodestate AS a LEFT JOIN proc_node AS b ON a.nodeid = b.id'.
			' LEFT JOIN proc_execution AS c ON a.executionid = c.id LEFT JOIN proc_workflow AS d ON a.workflowid=d.id'.
			' LEFT JOIN sys_user AS e on c.applyuserid=e.id WHERE a.`status` IN(0, 1) AND c.`status` = 1'.
			' AND d.`status` = 1 AND (a.acceptuid=%d OR a.acceptuserid=%d OR b.roleid IN(%s)'.
			' OR(b.roleid = 10 AND c.applyuserid = %d) OR (b.roleid = 8 AND e.orgid IN(%s))'.
			' OR (b.roleid = 9 AND e.orgid IN(%s)))';
		$sorm  = C\model::load('main.proc.nodestate')->get_orm();
		$esql  = sprintf($sql, 'count(1)', $userid, $userid, $rids, $userid, $cgids, $pgids);
		$total = $sorm->get_value($esql);
		$list  = array();
		if ($total > 0) {
			$status= $dlang->get('wk00002');
			$fields= 'a.executionid,b.`name`,d.`name` as workflow,a.`status`,c.applyusername as applyer, a.expire';
			$esql  = sprintf($sql, $fields, $userid, $userid, $rids, $userid, $cgids, $pgids);
			$list  = $sorm->get_list($esql, $start, $limit);
			foreach ($list as &$item) {
				$item['expire'] = doc_executor::datetime($item['expire']);
				$item['status'] = isset($status[$item['status']])? $status[$item['status']]:'UnKonw';
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	//工作流流程审批预览
	public function preview() {
		$view = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$id   = (int)input::get('id');
		$lang = C\lang::inst('adm');
		$data = C\model::load('main.proc.workflow')->get_one($id);
		if (empty($data)) {
			$view->assign_error('50010', $lang->get('workflow', '50010'));
		}
		$view->assign('title', $lang->get('workflow', '50011'));
		$view->assign_by_ref('workflow', $data);
		$view->display('workflow/preview.ext.js');
	}

	/**
	 * @desc    保存流程资料信息
	 * @doc     保存流程
	 * @access  需要授权
	 */
	public function dosave() {
		$view = C\view::inst('json');
		Cadmin::chkaccess($view);
		$id   = (int)input::get_post('id');
		$data = input::args();
		$data['svgxml']     = trim($data['svgxml']);
		$match= '<mxGraphModel>';
		if (strncasecmp($match, $data['svgxml'], strlen($match)) != 0) {
			$data['svgxml'] = '<mxGraphModel>'.$data['svgxml'].'</mxGraphModel>';
		}
		$data['stime'] = C\config::$nowtime;
		$orm  = C\model::load('main.proc.workflow');
		if ($id > 0) {
			$orm->save($id, $data);
		} else {
			$data['status']   = 1;
			$id = $orm->new_one($data);
		}
		$lang   = C\lang::inst('adm');
		$view->assign_by_ref('id', $id);
		$view->assign('msg', $lang->get('common', '40010'));
		$view->display();
	}

	/**
	 * @desc    流程代码生成器信息
	 * @doc     流程代码
	 * @access  需要授权
	 */
	public function docode() {
		$view = C\view::inst('json');
		Cadmin::chkaccess($view);
		$lang = C\lang::inst('adm');
		$workflowid = (int)input::get_post('id');
		if ($workflowid < 0) {
			$view->assign_error(1, $lang->get('workflow', '50012'));
		}
		$data = C\model::load('main.proc.workflow')->get_one($workflowid);
		if (empty($data)) {
			$view->assign_error(2, $lang->get('workflow', '50013'));
		}
		$domainfix= substr($_SERVER['SERVER_NAME'], strrpos($_SERVER['SERVER_NAME'], '.')+1);
		if ($domainfix != 'dev' && $domainfix != 'loc') {
			$view->assign_error(3, $lang->get('workflow', '50014'));
		}
		$index    = strrpos($data['clasz'], '\\');
		$namespace= substr($data['clasz'], 0, $index);
		$clasz    = substr($data['clasz'], $index + 1);
		$script   = "<?php\r\n";
		$script  .= "/**********************************************************\r\n";
		$script  .= " \t\t".$data['name']."\r\n";
		$script  .= " **********************************************************/\r\n";
		$script  .= "namespace ".$namespace.";\r\n";
		$script  .= "use \src\logic\workflow\doc_basic;\r\n";
		$script  .= 'class '.$clasz." extends doc_basic {\r\n";
		//获取节点记录的首个节点信息
		$norm     = C\model::load('main.proc.node');
		$where    = array('workflowid'=>$workflowid, 'preid'=>0);
		$node     = $norm->get_item($where);
		while ($node) {
			$where['preid'] = $node['id'];
			//拼凑函数方法名称
			$script .="\t//".$node['name']."\r\n";
			$script .= "\tpublic function ".$node['actions'].'(&$state) {'."\r\n";
			$node    = $norm->get_item($where);
			$result  = empty($node)? '-1':$node['result'];
			$script .= "\t\treturn '".$result."';\r\n";
			$script .= "\t}\r\n";	
		}
		$script.= "}\r\n";
		//生成的代码存储到文件当中
		$sfile  = COREDIR.'/'.str_replace('\\', '/', $namespace).'/'.$clasz.'.cls.php';
		if (file_exists($sfile)) {
			unlink($sfile);
		}
		file_put_contents($sfile, $script);
		$view->assign('msg', $lang->get('workflow', '50015'));
		$view->display();
	}
}